package com.tianpengtech.front.mobile.task;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.tianpengtech.common.model.task.Task;
import com.tianpengtech.common.model.user.User;

/**
 * 任务费用计算相关类
 * @author tianpengtech
 *
 */
public class TaskCalculateService {

	private double percent=0.98;
	private double clickPrice=0.3;
	public static TaskCalculateService ME = new TaskCalculateService();
	/**
	 * 计算，每个人接受任务后通过点击数来计算任务费用
	 * @param click_num
	 * @param task
	 * @return
	 */
	public int IShouldPayHowMuchMoney(long click_num,Task task){
		double actualFee=0;
		actualFee =  click_num *clickPrice;
		double totoalFee = calculateTaskTotalFee(task);
		actualFee = (actualFee>totoalFee)?totoalFee:actualFee;
		int actualFeeInt = (int)actualFee;
		return actualFeeInt;
	}
	
	public int AllTaskClickNumFee(long clickNum){
		double actualFee =  clickNum *clickPrice;//当前任务总花费
		int actualFeeInt = (int)actualFee;
		return actualFeeInt;
	}
	
	
	public double calculateTaskTotalFee(Task task){
		double totoalFee =task.getDouble(Task.TASK_PRICE)*task.getInt(Task.WH_NUMBER);
		totoalFee = (totoalFee==0)?task.getDouble(Task.TOTAL_FEE):totoalFee;
		return totoalFee;
	}
	
	
	/**
	 * 同步指定人的总收益数据信息
	 * @param totalFee
	 * @param userId
	 */
	public void syncTotalProfit(double totalFee,long userId){
		
		Record record = Db.findFirst("select * from fair_total_profit where wh_id=?",userId);
		if(record!=null){
			if(totalFee!=record.getDouble("profit")){
				int id = record.getInt("id");
				Db.update("update fair_total_profit set profit=?,wh_id=? where id =?",totalFee,userId,id);
			}
		}else{
			record = new Record();
			record.set("profit", totalFee);
			record.set("wh_id", userId);
			Db.save("fair_total_profit", record);
		}
	}
}
